package com.wrupple.muba.widget.client.widgets.panels;

import org.cobogw.gwt.user.client.CSS;

import com.google.gwt.event.dom.client.HasScrollHandlers;
import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.dom.client.ScrollHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.wrupple.muba.util.client.layout.IndexedLayoutDelegate;

public class PanelWithLayoutPositioner extends ResizeComposite  implements HasScrollHandlers{
	
	protected LayoutPanel panel;
	private IndexedLayoutDelegate indexedLayoutDelegate;

	public PanelWithLayoutPositioner(IndexedLayoutDelegate indexedLayoutDelegate) {
		super();
		this.indexedLayoutDelegate=indexedLayoutDelegate;
		panel = new LayoutPanel();
		indexedLayoutDelegate.setPanel(panel);
		CSS.setProperty(panel, "overflow", "auto");
		initWidget(panel);
	}
	
	


	protected void positionElement(IsWidget w, int i) {
		if(panel.getWidgetIndex(w)<0){
			panel.add(w);
		}
		indexedLayoutDelegate.positionElement(w.asWidget(),i);
	}
	
	
	public void clear() {
		panel.clear();
	}
	

	public void trim(int fromIndex, int toIndex) {
		for(; fromIndex<=toIndex ; fromIndex++){
			panel.remove(fromIndex);
		}
	}
	
	public void forceLayout() {
		panel.forceLayout();
	}
	
	public void animate(int milis){
		panel.animate(milis);
	}




	@Override
	public HandlerRegistration addScrollHandler(ScrollHandler handler) {
		return panel.addDomHandler(handler, ScrollEvent.getType());
	}




	public IndexedLayoutDelegate getIndexedLayoutDelegate() {
		return indexedLayoutDelegate;
	}

	public void setIndexedLayoutDelegate(IndexedLayoutDelegate indexedLayoutDelegate) {
		this.indexedLayoutDelegate = indexedLayoutDelegate;
	}

}
